package bairui.international.weixin.portal.miniapp.controller.branchCompany.staff;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.querydsl.QSort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.JPQLQuery;

import bairui.international.model.domain.jpa.QTB_BranchCompanyMoneyRecord;
import bairui.international.model.domain.jpa.TB_BranchCompanyMoneyRecord;
import bairui.international.model.domain.jpa.TB_BranchCompanyStaff;
import bairui.international.model.domain.jpa.TB_BranchCompanyStaffMoneyRecord;
import bairui.international.model.domain.jpa.TB_BranchCompanyStaffRoyaltyRecord;
import bairui.international.model.domain.jpa.TB_ProductOrder;
import bairui.international.model.domain.jpa.TB_User;
import bairui.international.model.type.Type_ProductOrderPay;
import bairui.international.service.Service_BranchCompanyMoneyRecord;
import bairui.international.service.Service_BranchCompanyStaff;
import bairui.international.service.Service_BranchCompanyStaffMoneyRecord;
import bairui.international.service.Service_User;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 *
 * 金额记录列表
 *
 * @generated
 * 
 * @see STest_C_BranchCompnay_Staff_MoneyRecoredPage_Get 测试
 *
 */
@Controller
public class C_BranchCompnay_Staff_MoneyRecoredPage_Get
 extends SC_BranchCompnay_Staff_MoneyRecoredPage_Get 
{
	
	@Autowired private Service_BranchCompanyStaff service_BranchCompanyStaff;
	@Autowired private Service_User service_User;
	@Autowired private Service_BranchCompanyMoneyRecord service_BranchCompanyMoneyRecord;
	@Autowired private Service_BranchCompanyStaffMoneyRecord service_BranchCompanyStaffMoneyRecord;
	private static final QTB_BranchCompanyMoneyRecord QBranchCompanyMoneyRecord = QTB_BranchCompanyMoneyRecord.tB_BranchCompanyMoneyRecord;
	
	/**
	 * Get请求
	 */
	@Action(Get.class)
	@GetMapping(Get.RequestMappingValue)
	public @FomatOutput @ResponseBody Object get(
		ModelMap map, HttpServletRequest request,
		@RequestParam Integer startPage,
		@RequestParam String skey
	) throws Exception {
		TB_User user = service_User.findOne(it->it.skey.eq(skey));
		TB_BranchCompanyStaff branchCompanyStaff = service_BranchCompanyStaff.getActive(user);
		//
		if(branchCompanyStaff.getDirectorFlag()){
			JPQLQuery<TB_BranchCompanyMoneyRecord> subQuery = JPAExpressions.selectFrom(QBranchCompanyMoneyRecord).where(QBranchCompanyMoneyRecord.productOrder.payWay.eq(Type_ProductOrderPay.Goods));
			//
			Page<TB_BranchCompanyMoneyRecord> page = service_BranchCompanyMoneyRecord.page(
					it->it.branchCompany.eq(branchCompanyStaff.getBranchCompany())
						.and(it.notIn(subQuery)), 
					PageRequest.of(startPage, 5),
					it->new QSort(it.id.desc())
				);
			return page.map(it->new Output(it));
		}else{
			Page<TB_BranchCompanyStaffMoneyRecord> page = service_BranchCompanyStaffMoneyRecord.page(
					it->it.branchCompanyStaff.eq(branchCompanyStaff),
					PageRequest.of(startPage, 5),
					it->new QSort(it.id.desc())
				);
			return page.map(it->new Output(it));
		}
	}
	
	
	@AllArgsConstructor
	public static class Output {
		private Double money;
		private Double currentMoney;
		private Double currentWithdrawMoney;
		private @Getter String createDate;
		private @Getter String typeComment;
		private @Getter String type;
		private @Getter String orderNumber;
		
		public Double getMoney() {
			return Double.parseDouble(new DecimalFormat("#.##").format(money));
		}
		
		public Double getCurrentMoney() {
			return Double.parseDouble(new DecimalFormat("#.##").format(currentMoney));
		}
		
		public Double getCurrentWithdrawMoney() {
			return Double.parseDouble(new DecimalFormat("#.##").format(currentWithdrawMoney));
		}
		
		public Output(TB_BranchCompanyMoneyRecord it){
			this.money = it.getMoney();
			this.currentMoney = it.getCurrentMoney();
			this.currentWithdrawMoney = it.getCurrentWithdrawMoney();
			this.createDate = null==it.getCreateTime() ? "" : new SimpleDateFormat("yyyy/MM/dd hh:mm:ss").format(it.getCreateTime());
			this.typeComment = it.getType().getComment();
			this.type = it.getType().name();
			TB_ProductOrder order = it.getProductOrder();
			if(null != order){
				this.orderNumber = order.getNumber();
			}
		}

		public Output(TB_BranchCompanyStaffMoneyRecord it) {
			this.money = it.getMoney();
			this.currentMoney = it.getCurrentMoney();
			this.currentWithdrawMoney = it.getCurrentWithdrawMoney();
			this.createDate = null==it.getCreateTime() ? "" : new SimpleDateFormat("yyyy/MM/dd hh:mm:ss").format(it.getCreateTime());
			this.typeComment = it.getType().getComment();
			this.type = it.getType().name();
			TB_BranchCompanyStaffRoyaltyRecord r = it.getBranchCompanyStaffRoyaltyRecord();
			if(null != r){
				this.orderNumber = r.getProductOrder().getNumber();
			}
		}
	}

}
